
package com.android.formula.task;

import java.util.concurrent.atomic.AtomicBoolean;

import android.content.Context;
import android.os.AsyncTask;

/**
 * Define common attributes and method of AsynTask.
 * 
 * @see AsyncTask
 */
public abstract class AbstractTask<TRequestParams, TProgress, TResult> extends
        AsyncTask<TRequestParams, TProgress, TResult> {

    /** use to notify the result to UI. */
    protected IDataEventHandler<TResult> mDataEventHandler;

    /** Interface to global information about an application environment. */
    protected Context mContext;

    /** Keep parameters for creating json request */
    protected TRequestParams mRequestParams;

    public AbstractTask(Context context, TRequestParams params,
            IDataEventHandler<TResult> dataEventHandler) {
        mDataEventHandler = dataEventHandler;
        mContext = context;
        mRequestParams = params;
    }

    /**
     * true: if want to cancel the thread.
     */
    protected final AtomicBoolean mShouldCancel = new AtomicBoolean(false);

    /**
     * check the thread is called cancel or not.
     * 
     * @return true if the thread is called cancel.
     */
    public boolean getShouldCancel() {
        return mShouldCancel.get();
    }

    /**
     * Used to cancel the thread.
     */
    public void setShouldCancel(boolean shouldCancel) {
        this.mShouldCancel.set(shouldCancel);
    }

    @Override
    protected void onPostExecute(TResult result) {
        if (mDataEventHandler != null) {
            mDataEventHandler.onNotifyErrorMessage("", "");
        }
        super.onPostExecute(result);
    }
}
